/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.guobo.dao.AssetDao;
import com.thinkgem.jeesite.modules.guobo.dao.ExhHallDao;
import com.thinkgem.jeesite.modules.guobo.dao.NCAssetDao;
import com.thinkgem.jeesite.modules.guobo.dao.TrailLogDao;
import com.thinkgem.jeesite.modules.guobo.entity.Asset;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.ExhStock;
import com.thinkgem.jeesite.modules.guobo.entity.ExhStockReport;
import com.thinkgem.jeesite.modules.guobo.entity.NCAsset;
import com.thinkgem.jeesite.modules.guobo.entity.TrailLog;

/**
 * 资产Service
 * 
 * @author wudavid
 * @version 2017-03-09
 */
@Service
@Transactional(readOnly = true)
public class AssetService extends CrudService<AssetDao, Asset> {

	@Autowired
	private TrailLogDao trailLogDao;

	@Autowired
	private ExhHallDao exhHallDao;

	@Autowired
	private NCAssetDao ncAssetDao;
	
	public Asset get(String id) {
		return super.get(id);
	}

	public List<Asset> findList(Asset asset) {
		return super.findList(asset);
	}

	public Page<Asset> findPage(Page<Asset> page, Asset asset) {
		return super.findPage(page, asset);
	}

	@Transactional(readOnly = false)
	public void save(Asset asset) {
		super.save(asset);
	}

	@Transactional(readOnly = false)
	public void delete(Asset asset) {
		super.delete(asset);
	}

	/**
	 * 查询没有设置预警规则的资产rfid标签
	 * 
	 * @return
	 */
	public Page<Asset> findRfidsNotInRole(Page<Asset> page, Asset asset) {
		asset.setPage(page);
		page.setList(dao.findRfidsNotInRole(asset));
		return page;
	}

	public Page<Asset> findAllAssets(Page<Asset> page, Asset asset) {
		asset.setPage(page);
		page.setList(dao.findAllAssets(asset));
		return page;
	}
	
	/**
	 * 根据展馆id查询展馆下的资产
	 * 
	 * @return
	 */
	public List<Asset> findAssetsByExHallId(String exhallId) {
		ExhHall exhHall = new ExhHall();
		exhHall.setId(exhallId);
		Asset asset = new Asset();
		asset.setExhHall(exhHall);
		return dao.findAssetsByExHallId(asset);
	}

	/**
	 * 查询资产的位置
	 * 
	 * @param page
	 * @param asset
	 * @return
	 */
	public Page<Asset> findAssetExhall(Page<Asset> page, Asset asset) {
		List<Asset> list = super.findList(asset);
		List<Asset> result = new ArrayList<Asset>();
		for (Asset at : list) {
			TrailLog trailLog = new TrailLog();
			trailLog.setRfid(at.getRfid());
			List<TrailLog> logs = trailLogDao.findListByRfid(trailLog);
			if (logs != null && logs.size() > 0) {
				String exhallId = logs.get(0).getRw().getLocation().getExhHall().getId();
				ExhHall exhall = exhHallDao.get(exhallId);
				at.setExhHall(exhall);
			}
			result.add(at);
		}
		asset.setPage(page);
		page.setList(result);
		page.setCount(result.size());
		return page;
	}

	/**
	 * 展馆非固定资产存量报表
	 * 
	 * @param page
	 * @param exhStockReport
	 * @param param
	 * @return
	 */
	public Page<ExhStockReport> findExhStockReport(Page<ExhStockReport> page, ExhStockReport exhStockReport,
			Map<String, Object> param) {
		String exhIds = param.get("exhIds") + "";
		// 查询哪些展馆存量
		String paramIds = "";
		if (exhIds!=null && !"null".equals(exhIds) && StringUtils.isNotBlank(exhIds)) {
			String[] tempIds = exhIds.split(",");
			for (String id : tempIds) {
				paramIds = paramIds+"'" + id + "'" + ",";
			}
		} else {
			List<ExhHall> allExhs = exhHallDao.findAllList(new ExhHall());
			for (ExhHall exhHall : allExhs) {
				paramIds = paramIds + "'" + exhHall.getId() + "'" + ",";
			}
		}
		paramIds = paramIds.substring(0, paramIds.length() - 1);
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("ids", paramIds);
		// 条件统计资产所在展馆的数量
		List<ExhStock> exhStocks = dao.getExhStocksByParam(condition);
		List<ExhStockReport> result = new ArrayList<ExhStockReport>();
		Map<String, Integer> tempExhStocks = new HashMap<String, Integer>();
		if (exhStocks != null && exhStocks.size() > 0) {
			for (ExhStock es : exhStocks) {
				String assetCode = es.getAssetCode();
				String exhHallId = es.getExhHall().getId();
				tempExhStocks.put(assetCode + "_" + exhHallId+"_"+es.getMandeptName(), es.getTotalNum());
			}
		}
		Asset asset = new Asset();
		String code = param.get("code") + "";
		asset.setCode(code);
		String name = param.get("name") + "";
		asset.setName(name);
		String spec = param.get("spec") + "";
		asset.setSpec(spec);
		String model = param.get("model") + "";
		asset.setModel(model);
		ExhHall exh = new ExhHall();
		exh.setId(paramIds);
		List<Asset> assets = dao.findAllMaterialAsset(asset);
		paramIds = paramIds.replace("'", "");
		if (assets != null && assets.size() > 0) {
			for (Asset ass : assets) {
				ExhStockReport tempExhStockReport = new ExhStockReport();
				tempExhStockReport.setAssetCode(ass.getCode());
				tempExhStockReport.setAssetName(ass.getName());
				tempExhStockReport.setAssetSpec(ass.getSpec());
				tempExhStockReport.setAssetModle(ass.getModel());
				tempExhStockReport.setMandeptName(ass.getMandeptName());
				String[] tempExhIds = paramIds.split(",");
				List<ExhStock> exhStockList = new ArrayList<ExhStock>();
				for (String exhId : tempExhIds) {
					ExhStock tempExhStock = new ExhStock();
					tempExhStock.setAssetCode(ass.getCode());
					tempExhStock.setAssetName(ass.getName());
					tempExhStock.setAssetSpec(ass.getSpec());
					tempExhStock.setAssetModle(ass.getModel());
					ExhHall exhHall = exhHallDao.get(exhId);
					tempExhStock.setExhHall(exhHall);
					Object totalNum = tempExhStocks.get(ass.getCode() + "_" + exhId+"_"+ass.getMandeptName());
					if (totalNum != null) {
						tempExhStock.setTotalNum((Integer) totalNum);
					} else {
						tempExhStock.setTotalNum(0);
					}
					exhStockList.add(tempExhStock);
				}
				 Collections.sort(exhStockList, new Comparator<ExhStock>(){  
					    @Override
			            public int compare(ExhStock o1, ExhStock o2) { 
			                //按照code升序排序
			                if(Integer.parseInt(o1.getExhHall().getCode()) > Integer.parseInt(o2.getExhHall().getCode())){  
			                    return 1;  
			                }  
			                if(Integer.parseInt(o1.getExhHall().getCode()) == Integer.parseInt(o2.getExhHall().getCode())){  
			                    return 0;  
			                }  
			                return -1;  
			            }					
			     });  
				tempExhStockReport.setExhStocks(exhStockList);
				Integer exhStockReportTotal = 0;
				for (ExhStock exhStock : exhStockList) {
					exhStockReportTotal = exhStockReportTotal + exhStock.getTotalNum();
				}
				tempExhStockReport.setTotalNum(exhStockReportTotal);
				result.add(tempExhStockReport);
			}
		}

		// 内容小于等于1页
		if (result.size() <= page.getPageSize() * 1) {
			page.setCount(result.size());
			page.setList(result);
			return page;
		}
		// 内容大于1页
		if (result.size() > page.getPageSize() * 1) {
			// 为第1页时
			if (page.getPageNo() == 1) {
				List<ExhStockReport> totals = result.subList(0, page.getPageSize() - 1);
				page.setCount(result.size());
				page.setList(totals);
				return page;
			}
			// 大于1页
			if (page.getPageNo() > 1) {
				// 不是最后页
				if (result.size() >= page.getPageSize() * page.getPageNo()) {
					List<ExhStockReport> totals = result.subList(page.getPageSize() * (page.getPageNo() - 1),
							page.getPageSize() * page.getPageNo() - 1);
					page.setCount(result.size());
					page.setList(totals);
					return page;
				}
				// 是最后页
				if (result.size() > page.getPageSize() * (page.getPageNo() - 1)
						&& result.size() <= page.getPageSize() * page.getPageNo()) {
					List<ExhStockReport> totals = result.subList(page.getPageSize() * (page.getPageNo() - 1),
							result.size() - 1);
					page.setCount(result.size());
					page.setList(totals);
					return page;
				}
			}
		}
		return null;
	}
	
	/**
	 * 分页查询NC资产信息
	 * @param page 页码
	 * @param pageSize 每页条数
	 * @param param 查询条件
	 * @return
	 */
	public List<NCAsset> findNCAssetsByPage(String page,String pageSize,Map<String,Object> param){
		int pageIndex = 1;
		int pageSizeDex=20;
		if(page!=null && !"".equals(page) && !"null".equals(page)){
			pageIndex = Integer.parseInt(page);
		}
		if(pageSize!=null && !"".equals(pageSize) && !"null".equals(pageSize)){
			pageSizeDex = Integer.parseInt(pageSize);
		}
		return ncAssetDao.findPageNCAssets(pageIndex, pageSizeDex,param);
	}
	
	/**
	 * 查询非固定资产
	 * @param page
	 * @param asset
	 * @return
	 */
	public Page<Asset> findMaterialAssetsPage(Page<Asset> page, Asset asset) {
		asset.setPage(page);
		page.setList(dao.findAllMaterialAsset(asset));
		return page;
	}
	
	/**
	 * 分页查询NC资产信息
	 * @param page 页码
	 * @param pageSize 每页条数
	 * @param param 查询条件
	 * @return
	 */
	public List<NCAsset> findNCMaterialAssetsByPage(String page,String pageSize,Map<String,Object> param){
		int pageIndex = 1;
		int pageSizeDex=20;
		if(page!=null && !"".equals(page) && !"null".equals(page)){
			pageIndex = Integer.parseInt(page);
		}
		if(pageSize!=null && !"".equals(pageSize) && !"null".equals(pageSize)){
			pageSizeDex = Integer.parseInt(pageSize);
		}
		return ncAssetDao.findPageNCMaterialAssets(pageIndex, pageSizeDex,param);
	}
	

	/**
	 * 展馆固定资产存量报表
	 * 
	 * @param page
	 * @param exhStockReport
	 * @param param
	 * @return
	 */
	public Page<ExhStockReport> findFixedAssetsExhStockReport(Page<ExhStockReport> page, ExhStockReport exhStockReport,
			Map<String, Object> param) {
		String exhIds = param.get("exhIds") + "";
		// 查询哪些展馆存量
		String paramIds = "";
		if (exhIds!=null && !"null".equals(exhIds) && StringUtils.isNotBlank(exhIds)) {
			String[] tempIds = exhIds.split(",");
			for (String id : tempIds) {
				paramIds = paramIds+"'" + id + "'" + ",";
			}
		} else {
			List<ExhHall> allExhs = exhHallDao.findAllList(new ExhHall());
			for (ExhHall exhHall : allExhs) {
				paramIds = paramIds + "'" + exhHall.getId() + "'" + ",";
			}
		}
		paramIds = paramIds.substring(0, paramIds.length() - 1);
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("ids", paramIds);
		// 条件统计资产所在展馆的数量
		List<ExhStock> exhStocks = dao.getFixedAssetsExhStocksByParam(condition);
		List<ExhStockReport> result = new ArrayList<ExhStockReport>();
		Map<String, Integer> tempExhStocks = new HashMap<String, Integer>();
		if (exhStocks != null && exhStocks.size() > 0) {
			for (ExhStock es : exhStocks) {
				String type = es.getType();
				String exhHallId = es.getExhHall().getId();
				tempExhStocks.put(type + "_" + exhHallId+"_"+es.getMandeptId(), es.getTotalNum());
			}
		}
		Asset asset = new Asset();
		String code = param.get("code") + "";
		asset.setCode(code);
		String name = param.get("name") + "";
		asset.setName(name);
		String spec = param.get("spec") + "";
		asset.setSpec(spec);
		String model = param.get("model") + "";
		asset.setModel(model);
		ExhHall exh = new ExhHall();
		exh.setId(paramIds);
		List<Asset> assets = dao.findAllAssetCode(asset);
		paramIds = paramIds.replace("'", "");
		if (assets != null && assets.size() > 0) {
			for (Asset ass : assets) {
				ExhStockReport tempExhStockReport = new ExhStockReport();
				tempExhStockReport.setType(ass.getType());
				tempExhStockReport.setTypeName(ass.getTypeName());
				tempExhStockReport.setAssetSpec(ass.getSpec());
				tempExhStockReport.setAssetModle(ass.getModel());
				tempExhStockReport.setMandeptName(ass.getMandeptName());
				tempExhStockReport.setMandeptId(ass.getMandeptId());
				String[] tempExhIds = paramIds.split(",");
				List<ExhStock> exhStockList = new ArrayList<ExhStock>();
				for (String exhId : tempExhIds) {
					ExhStock tempExhStock = new ExhStock();
					tempExhStock.setType(ass.getType());
					tempExhStock.setTypeName(ass.getTypeName());
					tempExhStock.setAssetSpec(ass.getSpec());
					tempExhStock.setAssetModle(ass.getModel());
					tempExhStock.setMandeptName(ass.getMandeptName());
					tempExhStock.setMandeptId(ass.getMandeptId());
					ExhHall exhHall = exhHallDao.get(exhId);
					tempExhStock.setExhHall(exhHall);
					Object totalNum = tempExhStocks.get(ass.getType() + "_" + exhId+"_"+ass.getMandeptId());
					if (totalNum != null) {
						tempExhStock.setTotalNum((Integer) totalNum);
					} else {
						tempExhStock.setTotalNum(0);
					}
					exhStockList.add(tempExhStock);
				}
				 Collections.sort(exhStockList, new Comparator<ExhStock>(){  
					    @Override
			            public int compare(ExhStock o1, ExhStock o2) { 
			                //按照code升序排序
			                if(Integer.parseInt(o1.getExhHall().getCode()) > Integer.parseInt(o2.getExhHall().getCode())){  
			                    return 1;  
			                }  
			                if(Integer.parseInt(o1.getExhHall().getCode()) == Integer.parseInt(o2.getExhHall().getCode())){  
			                    return 0;  
			                }  
			                return -1;  
			            }					
			     });  
				tempExhStockReport.setExhStocks(exhStockList);
				Integer exhStockReportTotal = 0;
				for (ExhStock exhStock : exhStockList) {
					exhStockReportTotal = exhStockReportTotal + exhStock.getTotalNum();
				}
				tempExhStockReport.setTotalNum(exhStockReportTotal);
				result.add(tempExhStockReport);
			}
		}

		// 内容小于等于1页
		if (result.size() <= page.getPageSize() * 1) {
			page.setCount(result.size());
			page.setList(result);
			return page;
		}
		// 内容大于1页
		if (result.size() > page.getPageSize() * 1) {
			// 为第1页时
			if (page.getPageNo() == 1) {
				List<ExhStockReport> totals = result.subList(0, page.getPageSize() - 1);
				page.setCount(result.size());
				page.setList(totals);
				return page;
			}
			// 大于1页
			if (page.getPageNo() > 1) {
				// 不是最后页
				if (result.size() >= page.getPageSize() * page.getPageNo()) {
					List<ExhStockReport> totals = result.subList(page.getPageSize() * (page.getPageNo() - 1),
							page.getPageSize() * page.getPageNo() - 1);
					page.setCount(result.size());
					page.setList(totals);
					return page;
				}
				// 是最后页
				if (result.size() > page.getPageSize() * (page.getPageNo() - 1)
						&& result.size() <= page.getPageSize() * page.getPageNo()) {
					List<ExhStockReport> totals = result.subList(page.getPageSize() * (page.getPageNo() - 1),
							result.size() - 1);
					page.setCount(result.size());
					page.setList(totals);
					return page;
				}
			}
		}
		return null;
	}
	
	/**
	 * 根据rfid查询资产
	 * @param rfid
	 * @return
	 */
	public Asset getAssetByRFID(String rfid){
		Asset asset = new Asset();
		asset.setRfid(rfid);
		return dao.getAssetByRfid(asset);
	}
	
	/**
	 * 根据父rfid查询所有子资产
	 * @param parentRfid 
	 * @param rfid
	 * @return
	 */
	public List<Asset> findAssetsByParentRfid(Asset asset){
		return dao.findAsstsByParentRfid(asset);
	}
}